//
//  HZOrderCellGoods.m
//  app
//
//  Created by 刘华舟 on 15/5/6.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HZOrderCellGoods.h"
#import "UIImageView+URL.h"

//view
#import "HZTableView.h"
#import "SNGiftAndPromotionView.h"

//model
#import "HZOrderCellModel.h"

static const NSInteger kOrderCellMargin = 24;


static const NSInteger kOrderGoodsPictureHeight = 160;
static const NSInteger kOrderGoodsNumLabelWidth = 120;
static const NSInteger kOrderGoodsNumLabelHeight = 60;

static const NSInteger kOrderCellPadding = 20;

@interface HZOrderCellGoods()

@property (strong, nonatomic) UIImageView *goodsPic;
@property (strong, nonatomic) UILabel *goodsTitleLabel;
@property (strong, nonatomic) UILabel *goodsPriceLabel;
@property (strong, nonatomic) UILabel* tagLabel;
@property (strong, nonatomic) UILabel *goodsCountLabel;
@property (strong, nonatomic) UIView *lineView;


@end

@implementation HZOrderCellGoods

+ (instancetype)cellWithTableView:(HZTableView *)tableView{
    
    HZOrderCellGoods *cell = [tableView dequeueReusableCellWithIdentifier:[HZOrderCellModel reuseIdentifierOfType:HZOrderCellTypeGoods]];
    if (cell == nil) {
        cell = [[HZOrderCellGoods alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[HZOrderCellModel reuseIdentifierOfType:HZOrderCellTypeGoods]];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.frame = CGRectOf([UBO_SCREEN fx:UB_LEFT fy:UB_TOP], [UBO_SCREEN fw:UB_WIDTH uh:[HZOrderCellModel cellUFHeightOfType:HZOrderCellTypeGoods]]);
        //设置背景色
        self.backgroundColor = HZ_WHITE_COLOR;
        self.contentMode = UIViewContentModeRedraw;
        
        //初始化图片按钮
        self.goodsPic = [[UIImageView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kOrderCellMargin fy:UB_TOP uy:kOrderCellMargin],
                                                                    [self.ubo uw:kOrderGoodsPictureHeight uh:kOrderGoodsPictureHeight])];
        self.goodsPic.backgroundColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR;
        self.goodsPic.layer.masksToBounds = YES;
        self.goodsPic.layer.borderColor = HZ_MALL_M_LIGHT_GRAY_BACKGROUND_COLOR.CGColor;
        self.goodsPic.layer.borderWidth = [self.ubo pw:2];
        //self.goodsPic.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.goodsPic];
        
        //初始化商品标题
        CGFloat goodsTitleWidth = [self.ubo fw:UB_WIDTH uw:-(2*kOrderCellMargin + kOrderCellPadding + kOrderGoodsPictureHeight)];
        self.goodsTitleLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsPic.ufo fx:UB_RIGHT ux:kOrderCellPadding fy:UB_TOP],
                                                                       [self.goodsPic.ubo ow:goodsTitleWidth fh:UB_HALF])];
        self.goodsTitleLabel.backgroundColor = HZ_CLEAR_COLOR;
        [self.goodsTitleLabel setTextColor:HZ_MALL_TEXT_BLACK_COLOR];
        self.goodsTitleLabel.textAlignment = NSTextAlignmentLeft;
        [self.goodsTitleLabel setFont:JKAppGlobleFontMenu];
        self.goodsTitleLabel.numberOfLines = 0;
        self.goodsTitleLabel.textAlignment = NSTextAlignmentLeft;
        [self addSubview:self.goodsTitleLabel];
        
        //初始化价格标签
        self.goodsPriceLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsPic.ufo fx:UB_RIGHT ux:kOrderCellPadding fy:UB_BOTTOM uy:-kOrderGoodsNumLabelHeight],
                                                                       [self.ubo fw:UB_WIDTH uw:-(kOrderGoodsNumLabelWidth + kOrderCellMargin*2 +kOrderCellPadding + kOrderGoodsPictureHeight) uh:kOrderGoodsNumLabelHeight])];
        self.goodsPriceLabel.textAlignment = NSTextAlignmentLeft;
        self.goodsPriceLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.goodsPriceLabel.textColor = HZ_MALL_TEXT_L_LIGHTGRAY_COLOR;
        self.goodsPriceLabel.font = JKAppGlobleFontContentText;
        [self addSubview:self.goodsPriceLabel];
        
        self.tagLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.goodsPriceLabel.ufo fx:UB_RIGHT ux:kOrderCellPadding fy:UB_TOP],
                                                                [self.ubo fw:UB_WIDTH uh:kOrderGoodsNumLabelHeight])];
        self.tagLabel.textAlignment = NSTextAlignmentLeft;
        self.tagLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.tagLabel.textColor = HZ_MALL_TEXT_L_LIGHTGRAY_COLOR;
        self.tagLabel.font = JKAppGlobleFontContentText;
        [self addSubview:self.tagLabel];
        
        //初始化购买数量标签
        self.goodsCountLabel = [[UILabel alloc] initWithFrame:CGRectOf([self.ufo fx:UB_RIGHT ux:-(kOrderGoodsNumLabelWidth+kOrderCellMargin) oy:self.goodsPriceLabel.ufo.y],
                                                                      [self.ufo uw:kOrderGoodsNumLabelWidth uh:kOrderGoodsNumLabelHeight])];
        self.goodsCountLabel.backgroundColor = HZ_CLEAR_COLOR;
        self.goodsCountLabel.textColor = HZ_MALL_TEXT_L_LIGHTGRAY_COLOR;
        self.goodsCountLabel.textAlignment = NSTextAlignmentRight;
        self.goodsCountLabel.font = JKAppGlobleFontContentText;
        [self addSubview:self.goodsCountLabel];
        

        self.lineView = [[UIView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_LEFT ux:kOrderCellMargin fy:UB_BOTTOM py:-1],
                                                               [self.ubo fw:UB_WIDTH ow:-kOrderCellMargin ph:1])];
        self.lineView.backgroundColor = HZ_MALL_M_SEPARATOR_CELL_LINE;
        [self addSubview:self.lineView];

        }
    
    return self;
}

-(void)displayCell:(HZOrderCellModel*)model showLine:(BOOL)isShow{
    self.model = model;
    
    
    //图像的动画特效
    [self.goodsPic sd_setImageWithURL:[NSURL URLWithString:model.titlePic] placeholderImage:nil options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
        
    } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (cacheType == SDImageCacheTypeNone){
            self.goodsPic.alpha = 0.0f;
            [UIView animateWithDuration:0.5f animations:^{
                self.goodsPic.alpha = 1.0f;
            }];
        }
    }];
    
    self.goodsTitleLabel.text = model.productTitle;
    
    self.goodsPriceLabel.text = [NSString stringWithFormat:@"￥%@", model.salePrice];
    [self.goodsPriceLabel sizeToFit];
    
    self.tagLabel.frame = CGRectOf([self.goodsPriceLabel.ufo fx:UB_RIGHT ux:kOrderCellPadding fy:UB_TOP],
                                                            [self.ubo fw:UB_WIDTH oh:self.goodsPriceLabel.frame.size.height]);
    self.tagLabel.text = model.shopName;
    [self.tagLabel sizeToFit];
    
    self.goodsCountLabel.text = [NSString stringWithFormat:@"x%@", model.productNum];
    [self.goodsCountLabel sizeToFit];
    
    self.goodsCountLabel.frame = CGRectOf([self.ufo fx:UB_RIGHT ux:-(kOrderCellMargin) ox:-self.goodsCountLabel.frame.size.width oy:self.goodsPriceLabel.ufo.y],
                                                                   self.goodsCountLabel.frame.size);
    
    self.lineView.hidden = !isShow;
    
}


@end
